package com.imti.controller;

import com.imti.model.Repo;
import com.imti.service.RepoService;
import com.imti.util.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@Slf4j
@RequestMapping("/repocontroller")
public class RepoController {
    @Resource
    RepoService repoService;

    @GetMapping("/page")
    public String findAllRepo(Repo repo, PageBean page, HttpServletRequest req){
        PageBean p= repoService.findAllRepo(repo,page);
        req.setAttribute("page",p);
        return "repo/repo";
    }
    @GetMapping("/addtiao")
    public String addtiao(){
        return "repo/addrepo";
    }

    @PostMapping("/addRepo")
    @ResponseBody
    public String addRepo(Repo repo){
        if (null == repo){
            return "{\"code\":\"0\"}";
        }
        Integer integer = repoService.addRepo(repo);
        if (integer == 0){
            return "{\"code\":\"0\"}";
        }
        return "{\"code\":\"1\"}";
    }
    @GetMapping("/{id}")
    public String getRepoById(@PathVariable("id" ) Integer id, HttpServletRequest req) {
        Repo repo = repoService.getRepoById(id);
        req.setAttribute("Repo", repo);
        return "repo/updaterepo";

    }
    @PostMapping("/update")
    @ResponseBody
    public String update(Repo repo){
        if (null == repo){
            return "{\"code\":\"0\"}";
        }
        Integer integer = repoService.updateRepo(repo);
        if (integer == 0){
            return "{\"code\":\"0\"}";
        }
        return "{\"code\":\"1\"}";
    }


}
